package com.hzy.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * Created by Administrator on 2016/1/20.
 */
public class RandomStringUtils {
    /**
     * 纯数字
     */
    public static final int ALL_NUMBER = 1;
    /**
     * 纯小写字母
     */
    public static final int ALL_LOWERCASE = 2;
    /**
     * 纯大写字母
     */
    public static final int ALL_CAPITAL = 3;
    /**
     * 纯字母
     */
    public static final int ALL_LETTER = 4;
    /**
     * 字母数字混合
     */
    public static final int LETTER_NUMBER = 7;

    private static Random RANDOM = new Random();

    public static String randomStr(int type, int length) {
        String resultStr = null;
        switch (type) {
            case ALL_NUMBER:
                resultStr = randomStr("1234567890", length);
                break;
            case ALL_LOWERCASE:
                resultStr = randomStr("qwertyuiopasdfghjklzxcvbnm", length);
                break;
            case ALL_CAPITAL:
                resultStr = randomStr("QWERTYUIOPASDFGHJKLZXCVBNM", length);
                break;
            case ALL_LETTER:
                resultStr = randomStr("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", length);
                break;
            case LETTER_NUMBER:
            default:
                resultStr = randomStr("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", length);
        }
        return resultStr;
    }

    public static String randomStr(String randString, int length) {
        if (length < 0 || randString == null) {
            return null;
        }
        String resultStr = "";
        int len = randString.length();
        for (int i = 0; i < length; ++i) {
            resultStr += randString.charAt(RANDOM.nextInt(len));
        }
        return resultStr;
    }

    /**
     * 从list中随机获取prizeNum个对象
     *
     * @param prizeNum
     * @return
     */
    public static List winnerPrizeUser(int prizeNum, List list) {
        int user_size = list.size();
        Random random = new Random();

        boolean[] bool = new boolean[user_size];
        int randInt = 0;
        List winner_users = new ArrayList();
        for (int i = 0; i < prizeNum; i++) {
            do {
                randInt = random.nextInt(user_size);
            } while (bool[randInt]);
            bool[randInt] = true;
            winner_users.add(list.get(randInt));
        }
        return winner_users;
    }


    /**
     * 生成订单号
     *
     * @return
     */
    public static String getOrderIdByUUId() {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
//         0 代表前面补充0
//         4 代表长度为4
//         d 代表参数为正数型
        return machineId + String.format("%015d", hashCodeV);
    }
}
